<!-- 驳回操作弹窗 -->
<template>
  <div>
    <el-dialog
      v-model="visible"
      width="30%"
      @close="onClose"
      :show-close="false"
    >
      <template #title>
        <p class="dialog-title">驳 回</p>
      </template>
      <body>
        <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          :rules="rules"
          label-width="0"
          class="demo-ruleForm"
          status-icon
        >
          <el-form-item label="" prop="content">
            <el-input
              v-model="ruleForm.content"
              type="textarea"
              placeholder="请输入驳回理由"
              :rows="5"
            />
          </el-form-item>
        </el-form>
      </body>
      <template #footer>
        <span class="dialog-footer">
          <el-button size="mini" @click="onClose">取 消</el-button>
          <el-button size="mini" type="primary" @click="submitForm(ruleFormRef)"
            >确 定</el-button
          >
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { ref, defineComponent, toRefs, reactive, watch } from "vue";
import { ElMessage } from "element-plus";
import type { FormInstance, FormRules } from "element-plus";
interface Props {
  dialogVisible: boolean;
}
interface State {
  visible: boolean;
}
export default defineComponent({
  name: "Dialog",
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
  },
  setup(props, context) {
    const { dialogVisible } = toRefs(props as Props);
    const parent = { ...context };
    const state = reactive<State>({
      visible: dialogVisible.value,
    });
    watch(
      dialogVisible,
      (newValue) => {
        state.visible = newValue;
      },
      { immediate: true }
    );
    const onClose = () => {
      parent.emit("update:dialogVisible", false);
    };
    const refreshTable = () => {
      parent.emit("refresh-table", "驳回操作");
    };
    const ruleFormRef = ref<FormInstance>();
    interface RuleForm {
      content: string;
    }
    const ruleForm = reactive<RuleForm>({
      content: "",
    });
    const rules = reactive<FormRules<RuleForm>>({
      content: [
        {
          required: true,
          message: "请输入驳回理由",
          trigger: "blur",
        },
      ],
    });
    const submitForm = async (formEl: FormInstance | undefined) => {
      if (!formEl) return;
      await formEl.validate((valid, fields) => {
        if (valid) {
          ElMessage({
            message: "驳回成功!",
            type: "success",
          });
          refreshTable();
          onClose();
          // ruleForm.content = "";
          console.log("submit!");
        } else {
          console.log("error submit!", fields);
        }
      });
    };
    return {
      ...toRefs(state),
      onClose,
      ruleForm,
      rules,
      submitForm,
      ruleFormRef,
      refreshTable,
    };
  },
});
</script>
<style lang="scss" scoped>
.dialog-title {
  font-size: 0.1rem;
  font-weight: bold;
  text-align: center;
}
</style>
